<?php
namespace app\houtai\controller\system\rbac;
use app\houtai\controller\BaseController;

class RoleController extends BaseController {
  /**
   *  @brief  : 后台账号角色
   *  @return : page
   *  @details:
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    ////////////////////////////////////////////////////////////////////////////
    $list = table('Ky_AdminRole a', 'rydb.websms')->field([
      'a.ID', 'a.Title', 'a.Remark', 'a.CreateTime', 'a.UpdateTime',
      '(SELECT COUNT (b.ID) FROM dbo.Ky_AdminRoleAction AS b LEFT JOIN dbo.Ky_AdminAction AS c ON b.NodeID = c.NavId WHERE b.State = 1 AND c.IsDelete = 0 AND b.RoleID = a.ID)' => 'TotalNode',
      '(SELECT COUNT (b.AdminId) FROM dbo.Ky_Admin AS b WHERE b.IsDisabled = 0 AND b.RoleID = a.ID)' => 'TotalUser',
    ])->where($conds)->order($order)->paginate();
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list' => $list,
    ]);
  }
  /**
   *  @brief  : 查看-修改-新增/角色
   *  @return : page|JSON
   *  @details:
   */
  public function detailAction() {
    $CHANNELS = GetAllChannel();
    $RoleID = $this->request->param('RoleID/d', 0);
    if ($this->request->isGet()) {
      $Role = [];
      $LimitChannel = [];
      if ($RoleID > 0) {
        $Role = table('Ky_AdminRole', 'rydb.websms')->where(['ID' => $RoleID])->find();
        if (empty($Role)) {
          return abort(1, "角色[RoleID={$RoleID}]不存在");
        }
        if (!empty($Role['LimitChannel'])) {
          $LimitChannel = trim($Role['LimitChannel']);
        }
        if (empty($LimitChannel)) {
          $LimitChannel = [];
        } else {
          $LimitChannel = explode(',', $LimitChannel);
          $LimitChannel = array_flip($LimitChannel);
        }
      }
      $Role['LimitChannel'] = $LimitChannel;
      return $this->fetch('', ['RoleID' => $RoleID, 'Role' => $Role, 'CHANNELS' => $CHANNELS]);
    }
    if ($this->request->isPost()) {
      $data = $this->request->only('Title,Remark');
      //--- 验证器验证 ---
      $retn = $this->validate($data, [ // 按照顺序逐一判断
        'Title|角色名称'  => 'require|max:255',
        'Remark|备注说明' => 'max:255',
      ]);
      if (true !== $retn) {
        return result([], 100, $retn);
      }

      $LimitChannel = '';
      $LC = $this->request->param('LC/a', []);
      $LC = array_unique($LC);
      if (!empty($LC)) {
        foreach($LC as $Channel) {
          if (!array_key_exists($Channel, $CHANNELS)) {
            return result([], 101, "渠道[{$Channel}]未定义");
          }
        }
        $LimitChannel = implode(',', $LC);
      }
      $data['LimitChannel'] = $LimitChannel;

      $table = table('Ky_AdminRole', 'rydb.websms');
      if ($RoleID > 0) { // 更新
        $retn = $table->where(['ID' => $RoleID])->update($data);
      } else { // 添加
        $retn = $table->insert($data);
      }
      return result([], 0);
    }
  }
  /**
   *  @brief  : 删除角色及权限
   *  @return :
   *  @details:NodeID   ActionID
   */
  public function deleteAction() {
    if (!$this->request->isPost()) {
      return result([], 100, '非法请求');
    }
    $RoleID = $this->request->param('RoleID/d', 0);
    if ($RoleID <= 0) {
      return result([], 101, '业务参数无效');
    }
    $Role = table('Ky_AdminRole', 'rydb.websms')->where(['ID' => $RoleID])->find();
    if (empty($Role)) {
      return result([], 102, "角色[RoleID={$RoleID}]不存在");
    }
    $Admin = table('Ky_Admin', 'rydb.websms')->where(['RoleID' => $RoleID, 'IsSys' => 1])->find();
    if (!empty($Admin)) {  // 避现
      return result([], 102, "角色[RoleID={$RoleID}]中有系统账号[{$Admin['UserName']}],删除失败!");
    }
    $eax = table('Ky_AdminRole', 'rydb.websms')->where(['ID' => $RoleID])->delete();
    $eax = table('Ky_AdminRoleAction', 'rydb.websms')->where(['RoleID' => $RoleID])->delete();
    $eax = table('Ky_Admin', 'rydb.websms')->where(['RoleID' => $RoleID])->setField('RoleID', 0);
    return result([], 0);
  }
}
